WARNING: This program has a logic error, so we have to debug it.
RULE: Every function needs to be called in order to execute. This includes the main function.
Click Run and you will see that nothing happens on the stage.
Fix the program so that the stage will be set. Note: Make sure every function is called!
Click Run to test if you fixed the program. When it is fixed, click Submit and Next.
To navigate the page using the TAB key, first press ESC to exit the code editor.
#This program will recreate the story of the 3 little pigs
def set_stage():
""" Sets up the stage for the start of the story """
stage.set_background("barn")
sprite = codesters.Sprite("pig",-100,-125)
def main():
""" Sets up the program and calls other functions """
set_stage()
t = codesters.Teacher()
defs = t.find_block("def")
mains = t.find_text("main")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
try:
tval1 = mains[1][1].replace(' ','').lower()
tval1_line_num = mains[1][0]
tval1_indent = t.get_indent_at_line(tval1_line_num)
except:
tval1 = "DNE"
tval1_line_num = "DNE"
tval1_indent = "DNE"
try:
tval1_def, tval1_distance = get_above_def(tval1_line_num, defs)
except:
tval1_def = "DNE"
tval1_distance = "DNE"
t1 = TestObjective()
t1.add_success(tval1 != "DNE", "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add a function call for the main() function?")
t2 = TestObjective()
t2.add_success("main" in tval1_def and tval1_indent == 0, "Great job!")
t2.add_failure("main" not in tval1_def, "Did you call main() after the main() function definition?")
t2.add_failure(tval1_indent > 0, "Make sure the main() function call is not indented!")
tester = TestManager()
tester.add_test_list([t1, t2])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.